package com.lyx.server.sms;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
/**
 * --- Be Humble and Hungry ---
 *
 * @author McMeloxD
 * @date 2024/8/12
 * @desc 阿里云短信工具类
 */
public class AliSmsUtil {
    public static final String TEM_RCODE= "SMS_471800183";
    public static final String KEY= "LTAI5t9SkTVecxz24SiFJxvo";
    public static final String SECRET= "4RfhxwWDyoj5gRZ11Vj3zGH0uMfE20";
    public static Client client;

    static {
        Config config = new Config()
                .setAccessKeyId(KEY)
                .setAccessKeySecret(SECRET);
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client= new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送短信验证码
     * @param tem 短信模板
     * @param phone 手机号
     * @param code 验证码*/
    public static boolean sendCode(String tem,String phone,String code){
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("McMeloxD")
                .setTemplateCode(tem)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response=client.sendSmsWithOptions(sendSmsRequest, runtime);
            return "OK".equals(response.getBody().code);
        } catch (Exception error) {
            error.printStackTrace();
        }
        return false;
    }
}
